import axios from 'axios'
import type { AxiosInstance, AxiosRequestConfig } from 'axios'
import config from './config'
// import qs from 'qs'
import { useUserStore } from '../store/user';
import { ElMessage } from 'element-plus'

const userStore = useUserStore()
function $axios(options: AxiosRequestConfig) {
    return new Promise<any>((resolve, reject) => {
        //创建新的axios
        const instance: AxiosInstance = axios.create({
            baseURL: config.baseUrl,
            headers: { Authorization: '' }
        })
        //请求拦截
        instance.interceptors.request.use(
            (res: any) => {
                let token = userStore.token
                if (token) {
                    res.headers.Authorization = token
                }
                return res
            },
            (err: any) => {
                reject(err)
            }
        )
        instance.interceptors.response.use(
            res => {
                if (res.data.msg) {
                    if (res.data.code == 0) {
                        ElMessage({
                            showClose: true,
                            message: res.data.msg,
                            type: 'warning',
                        })
                    } else {
                        ElMessage({
                            showClose: true,
                            message: res.data.msg,
                            type: 'success',
                        })
                    }
                }

                return res.data
            },
            err => {
                console.log(err);

                if (err.response.status) {
                    switch (err.response.status) {
                        // 403 token过期
                        // 登录过期对用户进行提示
                        // 清除本地token和清空vuex中token对象
                        case 403:
                            ElMessage({
                                showClose: true,
                                message: '登录过期，请重新登录',
                                type: 'error',
                            })
                            // 清除token
                            userStore.setToken('')
                            break;

                        // 404请求不存在
                        case 404:
                            ElMessage({
                                showClose: true,
                                message: '请求不到数据',
                                type: 'error',
                            })
                            break;
                        // 其他错误，直接抛出错误提示
                        default:
                            ElMessage({
                                showClose: true,
                                message: err.data.msg,
                                type: 'error',
                            })
                    }

                }
                Promise.reject(err.data.msg)
            }
        )
        instance(options).then(res => {
            resolve(res)
        }).catch(err => {
            console.log(err)
        })
    })
}


export default $axios